package com.ruoyi.web.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.page.PageUtils;
import com.ruoyi.common.utils.page.Query;
import com.ruoyi.web.domain.UcQuoQuotaOption;
import com.ruoyi.web.domain.UcQuoQuota;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.mapper.UcQuoQuotaOptionMapper;
import com.ruoyi.web.mapper.UcQuoQuotaMapper;
import com.ruoyi.web.service.IUcQuoQuotaService;
import com.ruoyi.web.utils.CodePrefixUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class UcQuoQuotaServiceImpl extends ServiceImpl<UcQuoQuotaMapper, UcQuoQuota> implements IUcQuoQuotaService {

    @Autowired
    private UcQuoQuotaOptionMapper ucQuoQuotaOptionMapper;

    @Override
    public PageUtils listByPage(QueryVo queryVo) {

        QueryWrapper<UcQuoQuota> qw =new QueryWrapper<>();

        if(queryVo.getQType()!=null){
            qw.eq("quota_type",queryVo.getQType());
        }
        if(queryVo.getQType1()!=null){
            qw.eq("choose_type",queryVo.getQType1());
        }
        if(!StringUtils.isEmpty(queryVo.getQKey())){
            qw.and((wrapper)->{
                wrapper.or().like("name",queryVo.getQKey());
            });
        }

        if("descending".equals(queryVo.getIsAsc())){
            qw.orderBy(true,false,queryVo.getOrderByColumn()).orderBy(true,false,"id");
        }else if("ascending".equals(queryVo.getIsAsc())){
            qw.orderBy(true,true,queryVo.getOrderByColumn()).orderBy(true,false,"id");
        }else{
            qw.orderBy(true,false,"id");
        }

        IPage<UcQuoQuota> page = this.page(new Query<UcQuoQuota>().getPage(BeanUtil.beanToMap(queryVo)), qw);

        return new PageUtils(page);
    }

    @Override
    public int insert(UcQuoQuota ucQuoQuota) {

        ucQuoQuota.setDelStatus(1);
        baseMapper.insert(ucQuoQuota);

        if(ucQuoQuota.getChooseType().equals(2) || ucQuoQuota.getChooseType().equals(3)){
            for (int i = 0; i < ucQuoQuota.getUcQuoQuotaOptions().size(); i++) {
                ucQuoQuota.getUcQuoQuotaOptions().get(i).setIndexNo(i+1);
                ucQuoQuota.getUcQuoQuotaOptions().get(i).setIndexStr(CodePrefixUtil.getIndexStr(CodePrefixUtil.NUMBER_INDEX,i));
                ucQuoQuota.getUcQuoQuotaOptions().get(i).setQuotaId(ucQuoQuota.getId());
                ucQuoQuota.getUcQuoQuotaOptions().get(i).setDelStatus(1);
                ucQuoQuotaOptionMapper.insert(ucQuoQuota.getUcQuoQuotaOptions().get(i));
            }
        }
        return 1;
    }

    @Override
    public int update(UcQuoQuota ucQuoQuota) {

        QueryWrapper<UcQuoQuotaOption> qwUcQuoQuotaOption = new QueryWrapper<>();
        qwUcQuoQuotaOption.eq("quota_id",ucQuoQuota.getId());
        List<UcQuoQuotaOption> ucQuoQuotaOptions = ucQuoQuotaOptionMapper.selectList(qwUcQuoQuotaOption);

        if(ucQuoQuota.getChooseType().equals(2) || ucQuoQuota.getChooseType().equals(3)){
            for (int i = 0; i < ucQuoQuota.getUcQuoQuotaOptions().size(); i++) {
                ucQuoQuota.getUcQuoQuotaOptions().get(i).setIndexNo(i+1);
                ucQuoQuota.getUcQuoQuotaOptions().get(i).setIndexStr(CodePrefixUtil.getIndexStr(CodePrefixUtil.NUMBER_INDEX,i));
            }
            List<UcQuoQuotaOption> addUcQuoQuotaOption = BeanUtils.compareDifference(
                    ucQuoQuota.getUcQuoQuotaOptions(),
                    UcQuoQuotaOption::getId,
                    ucQuoQuotaOptions,
                    UcQuoQuotaOption::getId
            );
            List<UcQuoQuotaOption> delUcQuoQuotaOption = BeanUtils.compareDifference(
                    ucQuoQuotaOptions,
                    UcQuoQuotaOption::getId,
                    ucQuoQuota.getUcQuoQuotaOptions(),
                    UcQuoQuotaOption::getId
            );
            List<UcQuoQuotaOption> updateUcQuoQuotaOption = BeanUtils.compareSame(
                    ucQuoQuota.getUcQuoQuotaOptions(),
                    UcQuoQuotaOption::getId,
                    ucQuoQuotaOptions,
                    UcQuoQuotaOption::getId
            );
            for (UcQuoQuotaOption ucQuoQuotaOption : addUcQuoQuotaOption) {
                ucQuoQuotaOption.setQuotaId(ucQuoQuota.getId());
                ucQuoQuotaOption.setDelStatus(1);
                ucQuoQuotaOptionMapper.insert(ucQuoQuotaOption);
            }
            for (UcQuoQuotaOption ucQuoQuotaOption : delUcQuoQuotaOption) {
                ucQuoQuotaOptionMapper.deleteById(ucQuoQuotaOption);
            }
            for (UcQuoQuotaOption ucQuoQuotaOption : updateUcQuoQuotaOption) {
                ucQuoQuotaOptionMapper.updateById(ucQuoQuotaOption);
            }
        }else{

            List<Long> ids = ucQuoQuotaOptions.stream().map(UcQuoQuotaOption::getId).collect(Collectors.toList());
            if(!StringUtils.isEmpty(ids)){
                ucQuoQuotaOptionMapper.deleteBatchIds(ids);
            }
        }


        baseMapper.updateById(ucQuoQuota);
        return 1;
    }
}
